package com.gexiaobao.pigeon.viewmodel.request

import androidx.lifecycle.MutableLiveData
import com.gexiaobao.pigeon.app.model.bean.PigeonBean
import com.gexiaobao.pigeon.app.model.bean.UiState
import com.gexiaobao.pigeon.app.network.ListDataUiState
import com.gexiaobao.pigeon.app.network.apiService
import com.gexiaobao.pigeon.app.util.RxToast
import me.hgj.jetpackmvvm.base.viewmodel.BaseViewModel
import me.hgj.jetpackmvvm.ext.request

/**
 * @Author      : hxw
 * @Date        : 2023/3/2 14:15
 * @Describe    :
 */
class RequestPigeonListViewModel : BaseViewModel() {

    private var pageNo: Int = 1

    //赛鸽列表信息
    var pigeonData: MutableLiveData<ListDataUiState<PigeonBean.RingInfo>> = MutableLiveData()

    //组织放下删除赛鸽
    var orgDeletePigeonResult: MutableLiveData<UiState> = MutableLiveData()

    fun getPigeonList(isRefresh: Boolean, type: String, ringId: String, userId: Int) {
        if (isRefresh) {
            pageNo = 1
        }
        val body = hashMapOf<String, Any>().apply {
            put("type", type)
            put("ps", 20)
            put("pn", pageNo)
            put("ringId", ringId)
            put("userId", userId)
        }
        request({ apiService.getPigeonList(body) }, {
            pageNo++
            val listDataUiState = ListDataUiState(
                isSuccess = true,
                isRefresh = isRefresh,
                isEmpty = it.isEmpty(),
                total = it.total,
                hasMore = it.hasMore(),
                isFirstEmpty = isRefresh && it.isEmpty(),
                listData = it.ringInfo
            )
            pigeonData.value = listDataUiState
        }, {
            val listDataUiState = ListDataUiState(
                isSuccess = false, errMessage = it.msg, errCode = it.code, isRefresh = isRefresh, listData = arrayListOf<PigeonBean.RingInfo>()
            )
            pigeonData.value = listDataUiState
        })
    }

    /** 组织方删除赛鸽 */
    fun orgDeletePigeon(id: String) {
        request({ apiService.orgDeletePigeon(id) }, {
            val result = UiState(isSuccess = true)
            orgDeletePigeonResult.value = result
        }, {
            val result = UiState(isSuccess = true, errorMsg = it.msg)
            orgDeletePigeonResult.value = result
        })
    }

}